// 正式地址
// const websiteUrl = 'https://fdqz.nomeis.cn/';  
// 测试地址
// const websiteUrl = 'http://zhanbu.com:88/';  
const websiteUrl = 'https://zhanbu.nomeis.cn/';
const appId = 'wxfa95b90c9d0f5b14';
const appSecret = 'b8f5cac6e7d5c667d13bfa4b3ef9e3ea';
let randText = [];
let downloadPosterBg = '';
let downloadHeaderImage = '';
let downloadQRImage = '';
let data = [{
	posterBackground: 'https://zhanbu.nomeis.cn/upload/page_resource/bg_not_kapai.jpg',
	posterHeadImage: 'https://zhanbu.nomeis.cn/upload/page_resource/kapai_face.png',
	posterQRImage: 'https://zhanbu.nomeis.cn/upload/page_resource/gh_4f2ea597d0ff_258.jpg'
}];
let globalData1;
const cardList0 = [{
	defaultCard: '../../static/img/card_def_one.png',
	activeCard: '../../static/img/card_act_one.png'
}, {
	defaultCard: '../../static/img/card_def_two.png',
	activeCard: '../../static/img/card_act_two.png'
}, {
	defaultCard: '../../static/img/card_def_three.png',
	activeCard: '../../static/img/card_act_three.png'
}, {
	defaultCard: '../../static/img/card_def_four.png',
	activeCard: '../../static/img/card_act_four.png'
}, {
	defaultCard: '../../static/img/card_def_five.png',
	activeCard: '../../static/img/card_act_five.png'
}];

const cardList1 = [{
	defaultCard: '../../static/img/card_def_one.png',
	activeCard: '../../static/img/card_act_one.png'
}, {
	defaultCard: '../../static/img/card_def_two.png',
	activeCard: '../../static/img/card_act_two.png'
}];
const cardList2 = [{
	defaultCard: '../../static/img/card_def_one.png',
	activeCard: '../../static/img/card_act_one.png'
}, {
	defaultCard: '../../static/img/card_def_two.png',
	activeCard: '../../static/img/card_act_two.png'
}, {
	defaultCard: '../../static/img/card_def_three.png',
	activeCard: '../../static/img/card_act_three.png'
}, {
	defaultCard: '../../static/img/card_def_four.png',
	activeCard: '../../static/img/card_act_four.png'
}, {
	defaultCard: '../../static/img/card_def_five.png',
	activeCard: '../../static/img/card_act_five.png'
}, {
	defaultCard: '../../static/img/card_def_six.png',
	activeCard: '../../static/img/card_act_six.png'
}, {
	defaultCard: '../../static/img/card_def_seven.png',
	activeCard: '../../static/img/card_act_seven.png'
}, {
	defaultCard: '../../static/img/card_def_et.png',
	activeCard: '../../static/img/card_act_et.png'
}, {
	defaultCard: '../../static/img/card_def_nin.png',
	activeCard: '../../static/img/card_act_nin.png'
}, {
	defaultCard: '../../static/img/card_def_ten.png',
	activeCard: '../../static/img/card_act_ten.png'
}];
//备用数组
const cardList3 = [{
	defaultCard: '../../static/img/card_def_one.png',
	activeCard: '../../static/img/card_act_one.png'
}, {
	defaultCard: '../../static/img/card_def_two.png',
	activeCard: '../../static/img/card_act_two.png'
}, {
	defaultCard: '../../static/img/card_def_three.png',
	activeCard: '../../static/img/card_act_three.png'
}, {
	defaultCard: '../../static/img/card_def_four.png',
	activeCard: '../../static/img/card_act_four.png'
}];


const cardList4 = [{
	defaultCard: '../../static/img/card_def_one.png',
	activeCard: '../../static/img/card_act_one.png'
}, {
	defaultCard: '../../static/img/card_def_two.png',
	activeCard: '../../static/img/card_act_two.png'
}, {
	defaultCard: '../../static/img/card_def_three.png',
	activeCard: '../../static/img/card_act_three.png'
}];





const login = function() {
	//如果未授权，进行授权，并且判断用户是否存在数据库，不存在则提示授权
	uni.login({
		provider: 'weixin',
		success: res => {
			// console.log('进入登录')
			// console.log(res)
			if (res.code) {
				getOpenid(res.code)
			} else {
				//用户按了拒绝按钮
				uni.showModal({
					title: '警告',
					content: '授权失败，请重新授权!!!',
					success: function(res) {}
				})
			}
		},
		fail: () => {},
		complete: () => {

		}
	});
}

const getOpenid = function(_code) {
	uni.request({
		url: websiteUrl + '?s=v1/getuseropenid/',
		method: 'POST',
		data: {
			'appid': appId,
			'appsecret': appSecret,
			'code': _code
		},
		success: res => {
			// console.log('获取openid')
			// console.log(res)
			globalData1 = res
			uni.setStorageSync('globalData', res)
		},
		fail: () => {},
		complete: () => {}
	});
}


// 弹出打卡海报
const showPoster = function() {
	uni.request({
		url: websiteUrl + '?s=v1/getconfig/',
		method: 'GET',
		data: {},
		success: res => {
			// 下载海报图
			uni.downloadFile({
				url: res.data.data.h5,
				success: function(res) {
					uni.setStorageSync('downloadPosterBg', res.tempFilePath)
					downloadPosterBg = res.tempFilePath;
				},
				fail: function() {
					console.log('fail');
				}
			});
			//下载二维码
			uni.downloadFile({
				url: res.data.data.spcode,
				success: function(res) {
					uni.setStorageSync('downloadQRImage', res.tempFilePath)
					downloadQRImage = res.tempFilePath;
				},
				fail: function() {
					console.log('fail');
				}
			});
		},
		fail: () => {},
		complete: () => {}
	});
}

// 弹出打卡海报
/**
 * @param {Object} e  海报地址
 */
const downLoadHb = function(e,url) {
	uni.request({
		url: websiteUrl + '?s=v1/getconfig/',
		method: 'GET',
		data: {},
		success: res => {
			// 下载海报图
			uni.downloadFile({
				url: e.thumb,
				success: function(res) {
					uni.setStorageSync('downloadPosterBg', res.tempFilePath)
					downloadPosterBg = res.tempFilePath;
				},
				fail: function() {
					console.log('fail');
				}
			});
			//下载二维码
			uni.downloadFile({
				url: res.data.data.spcode,
				success: function(res) {
					uni.setStorageSync('downloadQRImage', res.tempFilePath)
					downloadQRImage = res.tempFilePath;
					navTo(url)
				},
				fail: function() {
				}
			});
		},
		fail: () => {},
		complete: () => {
		}
	});
}


const navTo = function (e) {
	uni.navigateTo({
		url: e,
		success: res => {},
		fail: () => {},
		complete: () => {}
	});
}


export default {
	websiteUrl,
	appId,
	appSecret,
	login,
	getOpenid,
	randText,
	showPoster,
	downloadPosterBg,
	downloadQRImage,
	cardList1,
	cardList2,
	cardList0,
	cardList3,
	cardList4,
	globalData1,
	downLoadHb,
	navTo
}
